package com.example.bigevent.service.impl;

import com.example.bigevent.mapper.CategoryMapper;
import com.example.bigevent.pojo.Category;
import com.example.bigevent.pojo.PageBean;
import com.example.bigevent.service.CategoryService;
import com.example.bigevent.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增文章分类
     */
    @Override
    public void add(Category category) {
        if(category.getCreateUser()==null){
            Map<String,Object> map=ThreadLocalUtil.get();
            category.setCreateUser((Integer) map.get("id"));
        }
        categoryMapper.insert(category);
    }

    /**
     * 查询该用户所有文章分类
     */
    @Override
    public List<Category> list() {
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer userId= (Integer) map.get("id");
        return categoryMapper.selectByUserId(userId);
    }

    /**
     * 查询后台文章分类列表
     */
    @Override
    public PageBean<Category> sysList(Integer pageNum, Integer pageSize, Category category) {
        PageHelper.startPage(pageNum,pageSize);
        List<Category> list = categoryMapper.selectCategoryList(category);
        Page<Category> page= (Page<Category>) list;
        PageBean<Category> pageBean=new PageBean<>(page.getTotal(),page.getResult());
        return pageBean;
    }

    /**
     * 根据id查询文章分类详细信息
     */
    @Override
    public Category findById(Integer id) {
        return categoryMapper.findById(id);
    }

    /**
     * 查询用户所有文章分类
     */
    @Override
    public List<Category> listByUserId(Integer userId) {
        return categoryMapper.selectByUserId(userId);
    }

    /**
     * 更新文章分类
     */
    @Override
    public void update(Category category) {
        categoryMapper.update(category);
    }

    /**
     * 根据id删除文章分类
     */
    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteById(id);
    }
}
